<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>Bulk | Elasticsearch 权威指南</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    <link rel="next" href="../data/conclusion.html" />
    
    
    <link rel="prev" href="../data/mget.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="../gitbook/style.css">


        
    <div class="book"  data-level="3.11" data-basepath=".." data-revision="1436390985808">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                        <i class="fa fa-check"></i>
                        
                         Introduction
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1" data-path="getting_started/README.html">
            
                
                    <a href="../getting_started/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         入门
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="1.1" data-path="getting_started/what_is_it.html">
            
                
                    <a href="../getting_started/what_is_it.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                         初识
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.2" data-path="getting_started/installing_es.html">
            
                
                    <a href="../getting_started/installing_es.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                         安装
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.3" data-path="getting_started/api.html">
            
                
                    <a href="../getting_started/api.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                         API
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.4" data-path="getting_started/document.html">
            
                
                    <a href="../getting_started/document.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                         文档
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.5" data-path="getting_started/tutorial_indexing.html">
            
                
                    <a href="../getting_started/tutorial_indexing.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                         索引
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.6" data-path="getting_started/tutorial_search.html">
            
                
                    <a href="../getting_started/tutorial_search.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                         搜索
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.7" data-path="getting_started/tutorial_aggregations.html">
            
                
                    <a href="../getting_started/tutorial_aggregations.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                         汇总
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.8" data-path="getting_started/tutorial_conclusion.html">
            
                
                    <a href="../getting_started/tutorial_conclusion.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                         小结
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.9" data-path="getting_started/distributed.html">
            
                
                    <a href="../getting_started/distributed.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.9.</b>
                        
                         分布式
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.10" data-path="getting_started/conclusion.html">
            
                
                    <a href="../getting_started/conclusion.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.10.</b>
                        
                         本章总结
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="2" data-path="distributed_cluster/README.html">
            
                
                    <a href="../distributed_cluster/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                         分布式集群
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="2.1" data-path="distributed_cluster/empty_cluster.html">
            
                
                    <a href="../distributed_cluster/empty_cluster.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                         空集群
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.2" data-path="distributed_cluster/cluster_health.html">
            
                
                    <a href="../distributed_cluster/cluster_health.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                         集群健康
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.3" data-path="distributed_cluster/add_an_index.html">
            
                
                    <a href="../distributed_cluster/add_an_index.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                         添加索引
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.4" data-path="distributed_cluster/add_failover.html">
            
                
                    <a href="../distributed_cluster/add_failover.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                         容错移转
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.5" data-path="distributed_cluster/scale_horizontally.html">
            
                
                    <a href="../distributed_cluster/scale_horizontally.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                         横向扩展
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.6" data-path="distributed_cluster/scale_more.html">
            
                
                    <a href="../distributed_cluster/scale_more.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                         扩展
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.7" data-path="distributed_cluster/coping_with_failure.html">
            
                
                    <a href="../distributed_cluster/coping_with_failure.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                         故障恢复
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="3" data-path="data/README.html">
            
                
                    <a href="../data/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                         数据
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="3.1" data-path="data/document.html">
            
                
                    <a href="../data/document.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                         文档
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.2" data-path="data/index.html">
            
                
                    <a href="../data/index.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                         索引
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.3" data-path="data/get.html">
            
                
                    <a href="../data/get.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                         Get
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.4" data-path="data/exists.html">
            
                
                    <a href="../data/exists.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                         存在
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.5" data-path="data/update.html">
            
                
                    <a href="../data/update.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                         更新
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.6" data-path="data/create.html">
            
                
                    <a href="../data/create.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                         创建
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.7" data-path="data/delete.html">
            
                
                    <a href="../data/delete.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                         删除
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.8" data-path="data/version_control.html">
            
                
                    <a href="../data/version_control.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                         版本控制
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.9" data-path="data/partial_update.html">
            
                
                    <a href="../data/partial_update.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                         局部更新
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.10" data-path="data/mget.html">
            
                
                    <a href="../data/mget.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                         Mget
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter active" data-level="3.11" data-path="data/bulk.html">
            
                
                    <a href="../data/bulk.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.11.</b>
                        
                         Bulk
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.12" data-path="data/conclusion.html">
            
                
                    <a href="../data/conclusion.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.12.</b>
                        
                         总结
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="4" data-path="distributed_crud/README.html">
            
                
                    <a href="../distributed_crud/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                         分布式文档存储
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="4.1" data-path="distributed_crud/routing.html">
            
                
                    <a href="../distributed_crud/routing.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                         路由
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.2" data-path="distributed_crud/shard_interaction.html">
            
                
                    <a href="../distributed_crud/shard_interaction.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                         主从互通
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.3" data-path="distributed_crud/create_index_delete.html">
            
                
                    <a href="../distributed_crud/create_index_delete.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                         创建索引删除
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.4" data-path="distributed_crud/retrieving.html">
            
                
                    <a href="../distributed_crud/retrieving.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                         获取
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.5" data-path="distributed_crud/partial_updates.html">
            
                
                    <a href="../distributed_crud/partial_updates.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                         局部更新
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.6" data-path="distributed_crud/bulk_requests.html">
            
                
                    <a href="../distributed_crud/bulk_requests.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                         批量请求
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.7" data-path="distributed_crud/bulk_format.html">
            
                
                    <a href="../distributed_crud/bulk_format.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                         批量格式
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="5" data-path="search/README.html">
            
                
                    <a href="../search/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                         搜索
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="5.1" data-path="search/empty_search.html">
            
                
                    <a href="../search/empty_search.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                         空白搜索
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.2" data-path="search/multi_index_multi_type.html">
            
                
                    <a href="../search/multi_index_multi_type.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                         多索引多类型
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.3" data-path="search/pagination.html">
            
                
                    <a href="../search/pagination.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                         分页
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.4" data-path="search/query_string.html">
            
                
                    <a href="../search/query_string.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                         查询语句
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="6" data-path="mapping_analysis/README.html">
            
                
                    <a href="../mapping_analysis/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                         映射与统计
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="6.1" data-path="mapping_analysis/exact_vs_full_text.html">
            
                
                    <a href="../mapping_analysis/exact_vs_full_text.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                         Exact_vs_full_text
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.2" data-path="mapping_analysis/inverted_index.html">
            
                
                    <a href="../mapping_analysis/inverted_index.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                         Inverted_index
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.3" data-path="mapping_analysis/analysis.html">
            
                
                    <a href="../mapping_analysis/analysis.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                         Analysis
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.4" data-path="mapping_analysis/mapping.html">
            
                
                    <a href="../mapping_analysis/mapping.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                         Mapping
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.5" data-path="mapping_analysis/complex_datatypes.html">
            
                
                    <a href="../mapping_analysis/complex_datatypes.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                         Complex_datatypes
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Elasticsearch 权威指南</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_834">
                    
                        <h1 id="">批量更高效</h1>
<p>与<code>mget</code>能同时允许帮助我们获取多个文档相同，<code>bulk</code> API可以帮助我们同时完成执行多个请求，比如：<code>create</code>，<code>index</code>, <code>update</code>以及<code>delete</code>。当你在处理类似于log等海量数据的时候，你就可以一下处理成百上千的请求，这个操作将会极大提高效率。</p>
<p><code>bulk</code>的请求主体的格式稍微有些不同：</p>
<pre><code class="lang-js">{ action: { metadata }}\n
{ request body        }\n
{ action: { metadata }}\n
{ request body        }\n
...
</code></pre>
<p>这种格式就类似于一个用<code>&quot;\n&quot;</code>字符来连接的单行json一样。下面是两点注意事项：</p>
<ul>
<li>每一行都结尾处都必须有换行字符<code>&quot;\n&quot;</code>，<strong>最后一行也要有</strong>。这些标记可以有效地分隔每行。</li>
</ul>
<ul>
<li>这些行里不能包含非转义字符，以免干扰数据的分析 — — 这也意味着JSON<strong>不能</strong>是pretty-printed样式。</li>
</ul>
<hr>
<blockquote>
<h3 id="tip">TIP</h3>
</blockquote>
<p>在《bulk格式》一章中，我们将解释为何<code>bulk</code> API要使用这种格式。</p>
<hr>
<p><em>action/metadata</em> 行指定了将要在<strong>哪个文档</strong>中执行<strong>什么操作</strong>。</p>
<p>其中<em>action</em>必须是<code>index</code>, <code>create</code>, <code>update</code>或者<code>delete</code>。<em>metadata</em> 需要指明需要被操作文档的<code>_index</code>, <code>_type</code>以及<code>_id</code>，例如删除命令就可以这样填写：</p>
<pre><code class="lang-js">{ <span class="hljs-string">"delete"</span>: { <span class="hljs-string">"_index"</span>: <span class="hljs-string">"website"</span>, <span class="hljs-string">"_type"</span>: <span class="hljs-string">"blog"</span>, <span class="hljs-string">"_id"</span>: <span class="hljs-string">"123"</span> }}
</code></pre>
<p>在你进行<code>index</code>以及<code>create</code>操作时，<em>request body</em> 行必须要包含文档的<code>_source</code>数据——也就是文档的所有内容。</p>
<p>同样，在执行<code>update</code> API: <code>doc</code>, <code>upsert</code>,<code>script</code>的时候，也需要包含相关数据。而在删除的时候就不需要<em>request body</em>行。</p>
<pre><code class="lang-js">{ <span class="hljs-string">"create"</span>:  { <span class="hljs-string">"_index"</span>: <span class="hljs-string">"website"</span>, <span class="hljs-string">"_type"</span>: <span class="hljs-string">"blog"</span>, <span class="hljs-string">"_id"</span>: <span class="hljs-string">"123"</span> }}
{ <span class="hljs-string">"title"</span>:    <span class="hljs-string">"My first blog post"</span> }
</code></pre>
<p>如果没有指定<code>_id</code>，那么系统就会自动生成一个ID：</p>
<pre><code class="lang-js">{ <span class="hljs-string">"index"</span>: { <span class="hljs-string">"_index"</span>: <span class="hljs-string">"website"</span>, <span class="hljs-string">"_type"</span>: <span class="hljs-string">"blog"</span> }}
{ <span class="hljs-string">"title"</span>:    <span class="hljs-string">"My second blog post"</span> }
</code></pre>
<p>完成以上所有请求的<code>bulk</code>如下：</p>
<pre><code class="lang-js">POST /_bulk
{ <span class="hljs-string">"delete"</span>: { <span class="hljs-string">"_index"</span>: <span class="hljs-string">"website"</span>, <span class="hljs-string">"_type"</span>: <span class="hljs-string">"blog"</span>, <span class="hljs-string">"_id"</span>: <span class="hljs-string">"123"</span> }} &lt;<span class="hljs-number">1</span>&gt;
{ <span class="hljs-string">"create"</span>: { <span class="hljs-string">"_index"</span>: <span class="hljs-string">"website"</span>, <span class="hljs-string">"_type"</span>: <span class="hljs-string">"blog"</span>, <span class="hljs-string">"_id"</span>: <span class="hljs-string">"123"</span> }}
{ <span class="hljs-string">"title"</span>:    <span class="hljs-string">"My first blog post"</span> }
{ <span class="hljs-string">"index"</span>:  { <span class="hljs-string">"_index"</span>: <span class="hljs-string">"website"</span>, <span class="hljs-string">"_type"</span>: <span class="hljs-string">"blog"</span> }}
{ <span class="hljs-string">"title"</span>:    <span class="hljs-string">"My second blog post"</span> }
{ <span class="hljs-string">"update"</span>: { <span class="hljs-string">"_index"</span>: <span class="hljs-string">"website"</span>, <span class="hljs-string">"_type"</span>: <span class="hljs-string">"blog"</span>, <span class="hljs-string">"_id"</span>: <span class="hljs-string">"123"</span>, <span class="hljs-string">"_retry_on_conflict"</span> : <span class="hljs-number">3</span>} }
{ <span class="hljs-string">"doc"</span> : {<span class="hljs-string">"title"</span> : <span class="hljs-string">"My updated blog post"</span>} } &lt;<span class="hljs-number">2</span>&gt;
</code></pre>
<ol>
<li><p>注意<code>delete</code>操作是如何处理<em>request body</em>的,你可以在它之后直接执行新的操作。</p>
</li>
<li><p>请记住最后有换行符</p>
</li>
</ol>
<p>Elasticsearch会返回含有<code>items</code>的列表、它的顺序和我们请求的顺序是相同的：</p>
<pre><code class="lang-js">{
   <span class="hljs-string">"took"</span>: <span class="hljs-number">4</span>,
   <span class="hljs-string">"errors"</span>: <span class="hljs-literal">false</span>, <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">1</span>&gt;</span>
   "items": [
      {  "delete": {
            "_index":   "website",
            "_type":    "blog",
            "_id":      "123",
            "_version": 2,
            "status":   200,
            "found":    true
      }},
      {  "create": {
            "_index":   "website",
            "_type":    "blog",
            "_id":      "123",
            "_version": 3,
            "status":   201
      }},
      {  "create": {
            "_index":   "website",
            "_type":    "blog",
            "_id":      "EiwfApScQiiy7TIKFxRCTw",
            "_version": 1,
            "status":   201
      }},
      {  "update": {
            "_index":   "website",
            "_type":    "blog",
            "_id":      "123",
            "_version": 4,
            "status":   200
      }}
   ]
}}</span>
</code></pre>
<ol>
<li>所有的请求都被成功执行。</li>
</ol>
<p>每一个子请求都会被单独执行，所以一旦有一个子请求失败了，并不会影响到其他请求的成功执行。如果一旦出现失败的请求，<code>error</code>就会变为<code>true</code>，详细的错误信息也会出现在返回内容的下方：</p>
<pre><code class="lang-js">POST /_bulk
{ <span class="hljs-string">"create"</span>: { <span class="hljs-string">"_index"</span>: <span class="hljs-string">"website"</span>, <span class="hljs-string">"_type"</span>: <span class="hljs-string">"blog"</span>, <span class="hljs-string">"_id"</span>: <span class="hljs-string">"123"</span> }}
{ <span class="hljs-string">"title"</span>:    <span class="hljs-string">"Cannot create - it already exists"</span> }
{ <span class="hljs-string">"index"</span>:  { <span class="hljs-string">"_index"</span>: <span class="hljs-string">"website"</span>, <span class="hljs-string">"_type"</span>: <span class="hljs-string">"blog"</span>, <span class="hljs-string">"_id"</span>: <span class="hljs-string">"123"</span> }}
{ <span class="hljs-string">"title"</span>:    <span class="hljs-string">"But we can update it"</span> }
</code></pre>
<p>请求中的<code>create</code>操作失败，因为<code>123</code>已经存在，但是之后针对文档<code>123</code>的<code>index</code>操作依旧被成功执行：</p>
<pre><code class="lang-js">{
   <span class="hljs-string">"took"</span>: <span class="hljs-number">3</span>,
   <span class="hljs-string">"errors"</span>: <span class="hljs-literal">true</span>, <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">1</span>&gt;</span>
   "items": [
      {  "create": {
            "_index":   "website",
            "_type":    "blog",
            "_id":      "123",
            "status":   409, <span class="hljs-tag">&lt;<span class="hljs-title">2</span>&gt;</span>
            "error":    "DocumentAlreadyExistsException <span class="hljs-tag">&lt;<span class="hljs-title">3</span>&gt;</span>
                        [[website][4] [blog][123]:
                        document already exists]"
      }},
      {  "index": {
            "_index":   "website",
            "_type":    "blog",
            "_id":      "123",
            "_version": 5,
            "status":   200 <span class="hljs-tag">&lt;<span class="hljs-title">4</span>&gt;</span>
      }}
   ]
}</span>
</code></pre>
<ol>
<li>至少有一个请求错误发生。</li>
<li>这条请求的状态码为<code>409 CONFLICT</code>。</li>
<li>错误信息解释了导致错误的原因。</li>
<li>第二条请求的状态码为<code>200 OK</code>。</li>
</ol>
<p>这也更好地解释了<code>bulk</code>请求是独立的，每一条的失败与否 都不会影响到其他的请求。</p>
<h3 id="">能省就省</h3>
<p>或许你在批量导入大量的数据到相同的<code>index</code>以及<code>type</code>中。每次都去指定每个文档的metadata是完全没有必要的。在<code>mget</code> API中，<code>bulk</code>请求可以在URL中声明<code>/_index</code> 或者<code>/_index/_type</code>：</p>
<pre><code class="lang-js">POST /website/_bulk
{ <span class="hljs-string">"index"</span>: { <span class="hljs-string">"_type"</span>: <span class="hljs-string">"log"</span> }}
{ <span class="hljs-string">"event"</span>: <span class="hljs-string">"User logged in"</span> }
</code></pre>
<p>你依旧可以在metadata行中使用<code>_index</code>以及<code>_type</code>来重写数据，未声明的将会使用URL中的配置作为默认值：</p>
<pre><code class="lang-js">POST /website/log/_bulk
{ <span class="hljs-string">"index"</span>: {}}
{ <span class="hljs-string">"event"</span>: <span class="hljs-string">"User logged in"</span> }
{ <span class="hljs-string">"index"</span>: { <span class="hljs-string">"_type"</span>: <span class="hljs-string">"blog"</span> }}
{ <span class="hljs-string">"title"</span>: <span class="hljs-string">"Overriding the default type"</span> }
</code></pre>
<h3 id="">最大有多大？</h3>
<p>整个数据将会被处理它的节点载入内存中，所以如果请求量很大的话，留给其他请求的内存空间将会很少。<code>bulk</code>应该有一个最佳的限度。超过这个限制后，性能不但不会提升反而可能会造成宕机。</p>
<p>最佳的容量并不是一个确定的数值，它取决于你的硬件，你的文档大小以及复杂性，你的索引以及搜索的负载。幸运的是，这个<em>平衡点</em> 很容易确定：</p>
<p>试着去批量索引越来越多的文档。当性能开始下降的时候，就说明你的数据量太大了。一般比较好初始数量级是1000到5000个文档，或者你的文档很大，你就可以试着减小队列。
有的时候看看批量请求的物理大小是很有帮助的。1000个1KB的文档和1000个1MB的文档的差距将会是天差地别的。比较好的初始批量容量是5-15MB。</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../data/mget.html" class="navigation navigation-prev " aria-label="Previous page: Mget"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../data/conclusion.html" class="navigation navigation-next " aria-label="Next page: 总结"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
